﻿using SIAKAD.Helper;
using SIAKADCORE.Controllers;
using SIAKADCORE.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SIAKAD
{
    public partial class ViewPertanyaan : System.Web.UI.Page
    {
        private DataSet dsPertanyaan;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsCallback)
            {
                this.InitTabelPertanyaan();
            }
        }

        #region Helpers
        private void InitTabelPertanyaan()
        {
            bool isSuccess = PertanyaanController.GetTabel(out this.dsPertanyaan);
            if (isSuccess)
            {
                this.gvPertanyaan.DataSource = this.dsPertanyaan.Tables[0];
                this.gvPertanyaan.DataBind();
            }
        }

        protected void SetHiddenField(int index, ref HiddenField hiddenField)
        {
            string code = gvPertanyaan.DataKeys[index].Value.ToString();
            hiddenField.Value = code;
        }

        private void DetailCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfDetailID);
            var recent = this.GetRow(this.hfDetailID.Value);

            this.txtDetailPertanyaan.Text = recent.IsiPertanyaan;
            this.txtDetailPilihanJawaban1.Text = recent.PilihanJawaban1;
            this.txtDetailPilihanJawaban2.Text = recent.PilihanJawaban2;
            this.txtDetailPilihanJawaban3.Text = recent.PilihanJawaban3;
            this.txtDetailPilihanJawaban4.Text = recent.PilihanJawaban4;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowDetailModalScript", PertanyaanScript.ShowDetail(), false);
        }

        private void EditCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfUpdateID);
            var recent = this.GetRow(this.hfUpdateID.Value);

            this.txtEditPertanyaan.Text = recent.IsiPertanyaan;
            this.txtEditPilihanJawaban1.Text = recent.PilihanJawaban1;
            this.txtEditPilihanJawaban2.Text = recent.PilihanJawaban2;
            this.txtEditPilihanJawaban3.Text = recent.PilihanJawaban3;
            this.txtEditPilihanJawaban4.Text = recent.PilihanJawaban4;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowEditModalScript", PertanyaanScript.ShowEdit(), false);
        }

        private void DeleteCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfDeleteID);

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowDeleteModalScript", PertanyaanScript.ShowDelete(), false);
        }

        protected Pertanyaan GetRow(string idPertanyaan)
        {
            var recent = this.dsPertanyaan.Tables[0].Rows.OfType<DataRow>().FirstOrDefault(row => row[0].ToString() == idPertanyaan);

            return new Pertanyaan
            {
                IdPertanyaan = recent[0].ToString(),
                IsiPertanyaan = recent[1].ToString(),
                PilihanJawaban1 = recent[2].ToString(),
                PilihanJawaban2 = recent[3].ToString(),
                PilihanJawaban3 = recent[4].ToString(),
                PilihanJawaban4 = recent[5].ToString(),
            };
        } 
        #endregion

        #region Events
        protected void gvPertanyaan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("detailRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.DetailCommand(index);
            }
            else if (e.CommandName.Equals("updateRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.EditCommand(index);
            }
            else if (e.CommandName.Equals("deleteRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.DeleteCommand(index);
            }
        }

        protected void gvPertanyaan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvPertanyaan.PageIndex = e.NewPageIndex;
            this.InitTabelPertanyaan();

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "PagingScript", SiakadScript.PagerStyle(), false);
        }

        protected void btAdd_Click(object sender, EventArgs e)
        {
            this.txtAddPertanyaan.Text = string.Empty;
            this.txtAddPilihanJawaban1.Text = string.Empty;
            this.txtAddPilihanJawaban2.Text = string.Empty;
            this.txtAddPilihanJawaban3.Text = string.Empty;
            this.txtAddPilihanJawaban4.Text = string.Empty;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowAddModalScript", PertanyaanScript.ShowAdd(), false);
        }

        protected void btDeletePertanyaan_Click(object sender, EventArgs e)
        {
            bool isSuccess = PertanyaanController.Hapus(hfDeleteID.Value);
            if (isSuccess)
            {
                this.InitTabelPertanyaan();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideDeleteModalScript", PertanyaanScript.SuccessDelete(), false);
            }
        }

        protected void btEditPertanyaan_Click(object sender, EventArgs e)
        {
            var pertanyaan = new Pertanyaan
            {
                IdPertanyaan = this.hfUpdateID.Value,
                IsiPertanyaan = txtEditPertanyaan.Text,
                PilihanJawaban1 = txtEditPilihanJawaban1.Text,
                PilihanJawaban2 = txtEditPilihanJawaban2.Text,
                PilihanJawaban3 = txtEditPilihanJawaban3.Text,
                PilihanJawaban4 = txtEditPilihanJawaban4.Text,
            };

            bool isSuccess = PertanyaanController.Ubah(this.hfUpdateID.Value, pertanyaan);
            if (isSuccess)
            {
                this.InitTabelPertanyaan();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideEditModalScript", PertanyaanScript.SuccessEdit(), false);
            }
        }

        protected void btAddPertanyaan_Click(object sender, EventArgs e)
        {
            var pertanyaan = new Pertanyaan
            {
                IdPertanyaan = PertanyaanController.GetID(),
                IsiPertanyaan = txtAddPertanyaan.Text,
                PilihanJawaban1 = txtAddPilihanJawaban1.Text,
                PilihanJawaban2 = txtAddPilihanJawaban2.Text,
                PilihanJawaban3 = txtAddPilihanJawaban3.Text,
                PilihanJawaban4 = txtAddPilihanJawaban4.Text,
            };

            bool isSuccess = PertanyaanController.Tambah(pertanyaan);
            if (isSuccess)
            {
                this.InitTabelPertanyaan();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideAddModalScript", PertanyaanScript.SuccessAdd(), false);
            }
        } 
        #endregion

        
    }
}